<?php

namespace youmbe\media;

use Yii;
use yii\helpers\Url;
use common\helpers\Html;
use common\helpers\ArrayHelper;
use yii\helpers\Inflector;
use kartik\date\DatePicker;
use kartik\datetime\DateTimePicker;

/**
 * 生成图片
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class PictureGenerate extends \yii\base\Component {

    /**
     * 文字
     * @param array $options 配置
     * Renders the index view for the module
     * @return string
     */
    protected static function buildPhoto($photo, $text, $filename = null, $savepath = false, $view = false) {
        $photo .= '?' . rand();
        if (!@fopen($photo, 'r')) {
            Yii::error([
                '读取失败',
                $photo,
            ]);
            return false;
        }
        $ext = pathinfo($photo, PATHINFO_EXTENSION);
        list($width, $height) = getimagesize($photo);

        if (stristr($ext, 'jpg') !== FALSE || stristr($ext, 'jpeg') !== FALSE) {
            $src = imagecreatefromjpeg($photo);
        } elseif (stristr($ext, 'png') !== FALSE) {
            $src = imagecreatefrompng($photo);
        } elseif (stristr($ext, 'bmp') !== FALSE) {
            $src = imagecreatefrombmp($photo);
        } elseif (stristr($ext, 'gif') !== FALSE) {
            $src = imagecreatefromgif($photo);
        }

        $dst = imagecreatetruecolor(675, 420);
        imagesavealpha($dst, true);
        $trans_color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
        imagefill($dst, 0, 0, $trans_color);

        imagecopyresampled($dst, $src, 34, 29, 0, 0, 136, 179, $width, $height);

        static::textbox($dst, $text, [
            'size' => 14,
            'color' => 'ffffff',
            'coordinate' => [102, 190],
        ]);
        if ($savepath != false && is_string($savepath)) {
            $created = imagepng($dst, $savepath);
            if (file_exists($savepath) && $view != false) {
                header("Content-Type: image/png");
                $fp = fopen($savepath, 'rb');
                fpassthru($fp);
            }
            return $created;
        }

        if ($filename != null) {
            ob_start();
            imagepng($dst);
            $image = ob_get_contents();
            ob_end_clean();
            return Yii::$app->response->sendContentAsFile($image, $filename);
        }
        return $dst;
    }

    /**
     * 文字
     * @param array $options 配置
     * Renders the index view for the module
     * @return string
     */
    protected static function textbox($im, $text, $options = []) {
        $defaultOptions = [
            'font' => Yii::getAlias('@common') . '/data/fonts/simhei.ttf',
            'size' => 20,
            'angle' => 0,
            'color' => '000000',
            'coordinate' => [0, 0],
        ];
        $options = array_merge($defaultOptions, $options);
        extract($options);

        //字体颜色
        $rgb = \common\helpers\ColorHelper::hex2rgb($color);

        $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
        //盒子坐标
        $dimensions = ImageTTFBBox($size, $angle, $font, $text);

        $d_w = abs($dimensions[4] - $dimensions[0]);
        $d_h = abs($dimensions[5] - $dimensions[1]);

        //如设置了坐标，转为偏移量
        list($offset_x, $offset_y) = $coordinate;
        $x = $offset_x - $d_w / 2;
        $y = $offset_y + $d_h / 2;

        //设置内容
        ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $text);
    }

}
